package com.example.sample.activity;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;

import com.example.app.BaseActivity;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by LiangYu on 2018/5/2.
 */
public class WifiActivity extends BaseActivity {

    private Timer timer;
    private WifiManager wifiManager;
    private boolean enable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stop();
    }

    private void start(){
        if (timer == null){
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    turn();
                }
            }, 1000, 5000);
        }

    }

    private void stop(){
        if (timer != null){
            timer.cancel();
            timer = null;
        }
    }

    private void turn(){
        enable = !enable;
        wifiManager.setWifiEnabled(enable);
    }
}
